package com.warehouse.module.business.controller.app.account;

import com.warehouse.framework.common.pojo.CommonResult;
import com.warehouse.module.business.controller.app.account.vo.MpAccountCodeRequestVO;
import com.warehouse.module.business.controller.app.account.vo.MpAccountCodeResponseVO;
import com.warehouse.module.business.controller.app.account.vo.MpAccountRequestVO;
import com.warehouse.module.business.controller.app.account.vo.MpAccountUpdateRequestVO;
import com.warehouse.module.mp.api.MpUserInfoService;
import com.warehouse.module.mp.api.dto.MpUserDTO;
import com.warehouse.module.system.api.appuser.AppUserApi;
import com.warehouse.module.system.api.appuser.dto.AppUserDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

@RestController
@RequestMapping("/mp/account")
public class AppAccountController {

    @Resource
    private MpUserInfoService mpUserInfoService;

    @Resource
    private AppUserApi appUserApi;

    @GetMapping("/validCode")
    public CommonResult<MpAccountCodeResponseVO> validCode(@Valid MpAccountCodeRequestVO requestVO){
        String openId = mpUserInfoService.getMpUserInfo(requestVO.getAppId(), requestVO.getCode());
        AppUserDTO mpUserDTO = appUserApi.getAppUserByAppIdOpenId(requestVO.getAppId(), openId);
        if(mpUserDTO == null){
            MpAccountCodeResponseVO vo = new MpAccountCodeResponseVO();
            vo.setBindFlag(false);
            vo.setOpenId(openId);
            return CommonResult.success(vo);
        }else{
            MpAccountCodeResponseVO vo = new MpAccountCodeResponseVO();
            vo.setBindFlag(true);
            vo.setOpenId(openId);
            return CommonResult.success(vo);
        }
    }

    @GetMapping("/getUserInfo")
    public CommonResult<MpAccountCodeResponseVO> getUserInfo(@Valid MpAccountRequestVO requestVO){
        AppUserDTO mpUserDTO = appUserApi.getAppUserByAppIdOpenId(requestVO.getAppId(), requestVO.getOpenId());
        if(mpUserDTO == null){
            MpAccountCodeResponseVO vo = new MpAccountCodeResponseVO();
            vo.setBindFlag(false);
            vo.setOpenId(requestVO.getOpenId());
            return CommonResult.success(vo);
        }else{
            MpAccountCodeResponseVO vo = new MpAccountCodeResponseVO();
            vo.setBindFlag(true);
            vo.setOpenId(requestVO.getOpenId());
            return CommonResult.success(vo);
        }
    }

    @PostMapping("updatePhone")
    public CommonResult<Boolean> updatePhone(@RequestBody @Valid MpAccountUpdateRequestVO requestVO){
        return CommonResult.success(appUserApi.updateUserPhone(requestVO.getAppId(), requestVO.getOpenId(), requestVO.getPhone()));
    }

    @PostMapping("addAppUser")
    public CommonResult<Boolean> addAppUser(@RequestBody @Valid MpAccountUpdateRequestVO requestVO){
        return CommonResult.success(appUserApi.updateUserOpenId(requestVO.getAppId(), requestVO.getOpenId(), requestVO.getPhone()));
    }
}
